package com.yunxun.coinmessage.controller;

import com.yunxun.coinmessage.entity.Coin;
import com.yunxun.coinmessage.templatedata.MsgResult;
import com.yunxun.coinmessage.service.CoinService;
import com.yunxun.coinmessage.service.MesSourceService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

@Controller
public class CoinController {

    @Autowired
    CoinService coinService;
    @Autowired
    MesSourceService mesSourceService;
    @RequestMapping("/addCoin")
    @ResponseBody
    public MsgResult addCoin(@RequestBody @Valid Coin coin, BindingResult result)throws Exception{
        if(result.hasErrors()){
            throw new Exception(result.getFieldError().getDefaultMessage());
        }
        if(coin.getCname()==""){
            throw new Exception("币种名称不能为空");
        }
           return   coinService.addCoin(coin);

    }
    @RequestMapping("queryCoinById")
    public String queryById(Integer cid, Model model){
       Coin coin= coinService.queryById(cid);
       model.addAttribute("coin",coin);
       return "coin";
    }
    @RequestMapping("/deleteCoinById")
    @ResponseBody
    public MsgResult deleteCoinById(Integer cid)throws Exception{
           if(coinService.queryById(cid)==null){
               throw new Exception("你要删除的币种不存在");
           }
            return  coinService.deleteById(cid);
    }


    //分页查询方法
    @RequestMapping("/queryCoinByPage")
    public String queryByPage(@RequestParam("start")int start,@RequestParam("limit")int limit,Model model){

       List<Coin> coinList= coinService.queryByPage(start,limit);
       Integer total=coinService.queryTotal();
       Integer totalPage=(total+19)/20;
       model.addAttribute("total",total);
       model.addAttribute("totalPage",totalPage);
       model.addAttribute("coinList",coinList);
       return "coin-list";
    }
    //重复方法，带后面优化
    @RequestMapping("/queryCoinByPage1")
    @ResponseBody
    public List<Coin> queryByPage1(@RequestParam("start")int start,@RequestParam("limit")int limit){
        List<Coin> coinList= coinService.queryByPage(start,limit);
        Integer total=coinService.queryTotal();
        return coinList;
    }

    /**
     * 仅是更新币种
     *
     */
    @RequestMapping("/editCoinById")
    @ResponseBody
    public MsgResult editById(Coin coin)throws Exception{
        if(coin.getCname()==""){
            throw new Exception("修改币种名称不能为空");
        }
      return coinService.updateById(coin);
    }


    //打开编辑的窗口并回显数据
    @RequestMapping("/openEditCoinWindow")
    public String openEditWindow(Integer cid,Model model)throws Exception{
        if(coinService.queryById(cid)==null){
            throw new Exception("你要修改的币种不存在");
        }
        Coin coin=  coinService.queryById(cid);
        model.addAttribute("coin",coin);
        return "coin-edit";
    }
    //打开添加币种的窗口
    @RequestMapping("/openAddCoinWindow")
    public String openAddWindow(){
        return "coin-add";
    }

}
